﻿/**
 * @author sminutoli
 * version 1.0
 * 
 */
import com.dinmax.flash.utilities.movieclip.MovieClipUtils
import com.dinmax.flash.draw.common.*
import com.dinmax.flash.draw.cabeza.*


class ScrollCabeza
extends ControlCabeza
implements SelectedListener {
	
	public static var INTERVALDURATION:Number = 100;
	public static var DISABLED:String = "disabled";
	public static var ENABLED:String = "enabled";
	
	private var mcButtonRest:ButtonCabeza;
	private var mcButtonAdd:ButtonCabeza;
	private var mcScrollBar:ButtonCabeza;
	private var mcDrag:ButtonCabezaDraggable;
		
	private var intervalDuration:Number;
	
	private var state:String;
	private var isHorizontal:Boolean;
	private var mouseWheel:Boolean;
	private var mouseWheelTarget:MovieClip;
	private var scrollPos:Number;
	private var nStep:Number;
	
	function onLoad():Void{
		//trace("--	ScrollCabeza.as | onLoad()");
		//Si el parent es un Control le pido que me inicialice
		var parent:ControlCabeza = ControlCabeza(this._parent);
		if(parent) parent.initChild(this);
	};
	
	function selected(button:ControlCabeza):Void{
		switch(button){
			case this.mcButtonAdd:
			//trace("me tocaron el boton mas");
			this.setScrollPos(this.getScrollPos()+this.getStep());
			break;
			
			case this.mcButtonRest:
			//trace("me tocaron el boton rest");
			this.setScrollPos(this.getScrollPos()-this.getStep());
			break;
			
			case this.mcDrag:
			//trace("me tocaron el boton drag");
			this.setScrollPos(this.posToPercent(new Point(button._x, button._y)));
			break;
			
			case this.mcScrollBar:
			//trace("ScrollCabeza | me tocaron el boton barra");
			this.setScrollPos(this.posToPercent(new Point(button._xmouse, button._ymouse)));
			break;
			
		};
		return;
	};
	
	public function setDisabled():Void{
		//trace("--	ScrollCabeza.as | setDisabled()");
		if(this.state != ScrollCabeza.DISABLED){
			this.state = ScrollCabeza.DISABLED;
			this.gotoAndStop(ScrollCabeza.DISABLED);
			this.mcButtonAdd.setDisabled();
			this.mcButtonRest.setDisabled();
			this.mcDrag.setDisabled();
			this.mcScrollBar.setDisabled();	
		}
		return;
	};
	
	public function setEnabled():Void{
		//trace("--	ScrollCabeza.as | setEnabled()");
		if(this.state != ScrollCabeza.ENABLED){
			this.state = ScrollCabeza.ENABLED;
			this.gotoAndStop(ScrollCabeza.ENABLED);
			this.mcButtonAdd.setEnabled();
			this.mcButtonRest.setEnabled();
			this.mcDrag.setEnabled();
			
			this.mcScrollBar.setEnabled();	
		};
		return;
	};
	
	public function getScrollPos():Number {
		return scrollPos;
	};

	public function setScrollPos(percent:Number):Void {
		//trace("setScrollPos percent= "+percent);
		if(percent > 100) percent = 100;
		if(percent < 0) percent = 0;
		percent = Math.round(percent);
		this.scrollPos = percent;
		mcDrag.setPos(this.percentToPos(percent));
		this.selectEvent();
		return;
	};
	
	public function getIntervalDuration():Number {
		return intervalDuration;
	}

	public function setIntervalDuration(intervalDuration:Number):Void {
		this.intervalDuration = intervalDuration;
		this.mcButtonAdd.setIntervalDuration(intervalDuration);
		this.mcButtonRest.setIntervalDuration(intervalDuration);
		this.mcScrollBar.setIntervalDuration(intervalDuration);
		return;
	}

	public function getStep():Number {
		//trace("ScrollCabeza | getStep() | "+nStep);
		return nStep;
	}

	public function setStep(nStep:Number):Void {
		//trace("ScrollCabeza | setStep() | "+nStep);
		this.nStep = nStep;
		return;
	}
	
	public function setMouseWheel(mouseWheel:Boolean, target:MovieClip):Void {
		//trace("ScrollCabeza | mouseWheel= "+mouseWheel+" | target= "+target);
		this.mouseWheel = mouseWheel;
		if(target){
			this.setMouseWheelTarget(target);
		} else {
			this.setMouseWheelTarget(_level0);
		};
		
		if(mouseWheel){
			Mouse.addListener(this);
		} else {
			Mouse.removeListener(this);
		};
	}
	private function getMouseWheelTarget():MovieClip {
		return mouseWheelTarget;
	}

	private function setMouseWheelTarget(mouseWheelTarget:MovieClip):Void {
		//trace("ScrollCabeza.as path= "+targetPath(this));
		//trace("ScrollCabeza.as setMouseWheelTarget() "+mouseWheelTarget);
		this.mouseWheelTarget = mouseWheelTarget;
	}
	
	public function onMouseWheel(delta:Number):Void {
		if(delta < 0 && this.state == ScrollCabeza.ENABLED && MovieClipUtils.isMouseOver(this.getMouseWheelTarget())){
			this.setScrollPos(this.getScrollPos()+this.getStep());
		} else if(delta > 0 && this.state == ScrollCabeza.ENABLED && MovieClipUtils.isMouseOver(this.getMouseWheelTarget())) {
			
			this.setScrollPos(this.getScrollPos()-this.getStep());
		};
		return;
	};
		
	public function init():Void{
		this.setIntervalDuration(ScrollCabeza.INTERVALDURATION);
		//this.setScrollPos(0);
		
		this.mcButtonRest.addListener(this);
		this.mcButtonAdd.addListener(this);
		this.mcDrag.addListener(this);
		this.mcScrollBar.addListener(this);
		
		//trace("\nScrollCabeza.as | estoy haciendo un mcDrag.setLimitArea(this.getDragArea())");	
		mcDrag.setLimitArea(this.getDragArea());
				
		this.setIsHorizontal(this._width > this._height);
		return;
	};
	
	public function getState():String {
		return state;
	}

	public function setState(state:String):Void {
		this.state = state;
	}
	
	private function setIsHorizontal(is:Boolean):Void {
		this.isHorizontal = is;
		return;
	};
	
	private function getIsHorizontal():Boolean {
		return this.isHorizontal;
	};
		
	private function percentToPos(percent:Number):Point{
		//trace("ScrollCabeza.as | percentToPos "+percent);
		var init:Point = this.mcDrag.getLimitArea().getInitXY();
		var pos:Point = new Point(init.getX(), init.getY());
			
		if(this.getIsHorizontal()){
			var availWidth:Number = this.mcScrollBar._width - this.mcDrag._width;
			var nValue:Number = this.mcScrollBar._x + (percent * availWidth / 100);
			pos.setX(nValue);
		} else {
			var availHeight:Number = this.mcScrollBar._height - this.mcDrag._height;
			var nValue:Number = this.mcScrollBar._y + (percent * availHeight / 100);
			pos.setY(nValue);
		};
		return pos;
	};
	
	private function posToPercent(pos:Point):Number {
		if(this.getIsHorizontal()){
			var availWidth:Number = this.mcScrollBar._width - this.mcDrag._width;
			var dif:Number = pos.getX() - this.mcScrollBar._x;
			var percent:Number= ( dif ) * 100 / availWidth;
		} else {
			var availHeight:Number = this.mcScrollBar._height - this.mcDrag._height;
			var dif:Number = pos.getY() - this.mcScrollBar._y;
			var percent:Number= (dif ) * 100 / availHeight;
		};
		
		return percent;
	};
	
	private function getDragArea():Area {
		var pInit:Point = new Point(mcScrollBar._x, mcScrollBar._y);
		var pEnd:Point = new Point(mcScrollBar._x+mcScrollBar._width - mcDrag._width, mcScrollBar._y+mcScrollBar._height - mcDrag._height);
		var area:Area = new Area(pInit, pEnd);
		return area;
	};

}